<?php


class EmsElementFooterForm extends BaseEmsElementForm
{
    public function configure()
    {
        unset(
            $this['created_at'], $this['updated_at'], $this['published'],
            $this['is_enabled'], $this['section_id']
        );

        $this->setWidget('type_id', new sfWidgetFormInputHidden());

        if ($this->object->exists())
        {
            $this->widgetSchema['published'] = new sfWidgetFormInputCheckbox(array('label' => 'Publicar'));
            $this->validatorSchema['published'] = new sfValidatorBoolean();
            $this->widgetSchema['is_enabled'] = new sfWidgetFormInputCheckbox(array('label' => 'Habilitar'));
            $this->validatorSchema['is_enabled'] = new sfValidatorBoolean();
        }

        $this->widgetSchema->setLabels(array('title' => 'Nombre'));

        switch ($this->getObject()->getType()->getType()) {
            case 'Menu':
                if(!$this->getObject()->getMenu()){
                    $menu = new EmsMenu();
                    $this->getObject()->setMenu($menu);
                }
                $form = new EmsMenuForm($this->getObject()->getMenu(),
                        array('portal_id' => $this->getOption('portal_id')));
                $this->embedForm('Menu', $form);
                break;
            case 'Banner':
                if(!$this->getObject()->getBanner()){
                    $banner = new EmsBanner();
                    $this->getObject()->setBanner($banner);
                }

                $form = new EmsBannerForm($this->getObject()->getBanner(),
                        array('portal_id' => $this->getOption('portal_id')));
                $this->embedForm('Banner', $form);
                break;
            default:
                break;
        }


    }

    public function addMenuOption ($num)
    {
        $form = $this->getEmbeddedForm('Menu');
        $form->addOption($num);
        $this->embedForm('Menu', $form);
    }

    public function createElementStructure(EmsElementType $type, array $taintedValues = null, array $taintedFiles = null)
    {
        $to_remove = array ();
        switch ($type->getType()){
            case 'Menu':
                foreach ($taintedValues['Menu']['Options'] as $i => $option)
                {
                    if (!isset($this['Menu']['Options'][$i]) && ($option['title'] !== '' || $option['link'] !== ''))
                    {
                        $this->addMenuOption($i);
                    } elseif ($option['title'] == '' && $option['link'] == '') {
                        array_push($to_remove,$i);
                    }
                }
                return $to_remove;
            default:
                break;
        }
    }

    public function bind(array $taintedValues = null, array $taintedFiles = null)
    {
        $to_remove = array ();
        if (isset($taintedValues[$this->getObject()->getType()->getType()]))
        {
            $to_remove = $this->createElementStructure($this->getObject()->getType(), $taintedValues, $taintedFiles);
        }

        switch ($this->getObject()->getType()->getType()) {
            case 'Menu':
                foreach ($to_remove as $key ) {
                    unset ($taintedValues['Menu']['Options'][$key]);
                }
                break;
            default:
                break;
        }

        parent::bind($taintedValues, $taintedFiles);
    }
}